import collections
class Solution:
    def countCharacters(self, words, chars: str) -> int:
        chars_cnt=collections.Counter(chars)
        ans=0
        for word in words:
            word_cnt=collections.Counter(word)
            for i in word_cnt:
                if word_cnt[i]>chars_cnt[i]:
                    break
            else:
                ans+=len(word)
        return print(ans)

words=["cat","bt","hat","tree"]
chars="atach"

Solution().countCharacters(words,chars)
